package my.board;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import my.climbing.R;
import my.db.ReviewVO;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

public class NewReviewActivity extends ListActivity implements View.OnClickListener{
	
	Button into,b1;
	EditText et;
	
	ArrayList<ReviewVO> list=new ArrayList<ReviewVO>();
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newreview);
        getData();
		setListAdapter(new MyListAdapter(this, R.layout.row, list));
	
		into=(Button)findViewById(R.id.into);
		
		into.setOnClickListener(this);
		
		b1=(Button)findViewById(R.id.findbut);
	b1.setOnClickListener(this);
	
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	
			if(v.getId()==R.id.into)
			{
				Intent i=new Intent();
				i.setClass(this,SanWrite.class);
				startActivity(i);
			
			}
	
			if(v.getId()==R.id.findbut)
			 {
				Intent i=new Intent();
				i.setClass(this, FindSanReview.class);
				startActivity(i);
				}
		
	}
	public String downLoadJSON()
	{
	 	StringBuilder sb=new StringBuilder();
		
		try
		{
			String strUrl="http://211.238.142.88:8080/Review/review/review.jsp";
		   URL url=new URL(strUrl);
		   HttpURLConnection conn=(HttpURLConnection)url.openConnection();
		  
		   if(conn!=null)
		   {
			   if(conn.getResponseCode()==HttpURLConnection.HTTP_OK)
			 {
			 
			   BufferedReader in=new BufferedReader(
					   new InputStreamReader(conn.getInputStream()));
			   String str=""; 
			   while(true)
			   {
				   str=in.readLine();
				   sb.append(str);
				   if(str==null)
				break;
			   }
			   in.close();
			   
		
		   }
		   }
		}
		   catch(Exception ex){}
		
		
		   return sb.toString();
	}
public void getData()
{
	  try
	  {
		  String json=downLoadJSON();
		  JSONArray ja=new JSONArray(json);
	  
	   for(int i=0;i<ja.length();i++)
	   {
		   JSONObject obj=ja.getJSONObject(i);
		   ReviewVO vo=new ReviewVO();
		   vo.setNo(obj.getInt("no"));
		   vo.setWriteday(obj.getString("writeday"));
		   vo.setSan(obj.getString("san"));
		   vo.setName(obj.getString("name"));
		   vo.setJumsu(obj.getInt("jumsu"));
		   vo.setContent(obj.getString("content"));
		   vo.setSubject(obj.getString("subject"));
		   vo.setPwd(obj.getString("pwd"));
		   vo.setSanimage(obj.getString("sanimage"));
		   
	
		   list.add(vo);
	   }
	   
	  }
	  catch(Exception ex)
	  {
		  
	  }
}
	@Override
	 protected void onListItemClick(ListView l, View v, int position, long id) {
	  // TODO Auto-generated method stub
	  super.onListItemClick(l, v, position, id);
	  
	 Intent i=new Intent();
	 TextView no=(TextView)v.findViewById(R.id.review_no);
	 String strNo=no.getText().toString();
	 i.setClass(this, ContentActivity.class);
	 i.putExtra("no",strNo);
	 startActivity(i);
	 
	 }

	
	}


	
	



class MyListAdapter extends BaseAdapter
{

	ArrayList<ReviewVO> list=new ArrayList<ReviewVO>();
	int layout;
	LayoutInflater in;
	Context mc;
	
	public MyListAdapter(Context c,int lay,ArrayList<ReviewVO> list)
	{
		
		this.layout=lay;
		mc=c;
		this.list=list;
		in=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup viewGroup) {
		// TODO Auto-generated method stub
		if(convertView==null)
		
			convertView=in.inflate(layout, null);
		
		ReviewVO vo=list.get(pos);
		TextView t1=(TextView)convertView.findViewById(R.id.review_name);
		t1.setText(vo.getName());
		TextView t2=(TextView)convertView.findViewById(R.id.review_subject);
		t2.setText(vo.getSubject());
		TextView t3=(TextView)convertView.findViewById(R.id.review_date);
		t3.setText(vo.getWriteday());
		TextView t4=(TextView)convertView.findViewById(R.id.review_no);
		t4.setText(String.valueOf(vo.getNo()));
		RatingBar rb=(RatingBar)convertView.findViewById(R.id.review_rating);
		
		rb.setRating((float)vo.getJumsu());
		ImageView iv=(ImageView)convertView.findViewById(R.id.san_image);
		
		try
		{
			String str="http://211.238.142.88:8080/Review/san/"
		                 +vo.getSanimage()+".png";
			URL url=new URL(str);
			HttpURLConnection hc=(HttpURLConnection)url.openConnection();
			InputStream is=hc.getInputStream();
			BufferedInputStream bi=new BufferedInputStream(is);
			Bitmap bm=BitmapFactory.decodeStream(bi);
			
			
			is.close();
			bi.close();
			iv.setImageBitmap(bm);
		}catch(Exception ex){}
		return convertView;
	}
}
		